package com.redwood.report.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.duowan.mobile.netroid.Listener;
import com.duowan.mobile.netroid.NetroidError;
import com.redwood.report.R;
import com.redwood.report.common.Constant;
import com.redwood.report.entity.UpdateInfo;
import com.redwood.report.net.download.DownloadManager;
import com.redwood.report.util.FileProvider7;

import java.io.File;

/**
 * 自定义弹出框 升级
 * Created by daiye on 2016/12/26 0026.
 */

public class CUpdateDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private UpdateInfo update;
    private TextView content;

    public CUpdateDialog(Context context, UpdateInfo update) {
        super(context, R.style.select_dialog);
        this.context = context;
        this.update =update;
    }

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置没有标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.dialog_update);

        ((TextView)findViewById(R.id.alertTitle)).setText(getContext().getString(R.string.update_title) + update.getVersionCode() );
        content = ((TextView)findViewById(R.id.content));
        content.setText(getContext().getString(R.string.update_content));
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.tv_download).setOnClickListener(this);
        if(update.isIsupdate()) {
            setCanceledOnTouchOutside(false);
            setOnKeyListener(new OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_download:
                downloadApk(Constant.getBaseUrl()+update.getFilePath());
                break;
            case R.id.tv_cancel:
                if(update.isIsupdate()) {
                    ((Activity)context).finish();
                }
                this.dismiss();
                break;
        }
    }

    @Override
    public void setTitle(CharSequence title) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void show() {
        if (context != null && ((Activity) context).isFinishing()) {
            return;
        }
        setCanceledOnTouchOutside(true);//点击对话框外部取消对话框显示
        super.show();
    }

    private void downloadApk(String url) {
        DownloadManager.getInstance().download(DownloadManager.UPDATE, url, new Listener<Void>() {
            @Override
            public void onSuccess(Void response) {
                CUpdateDialog.this.dismiss();
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(Intent.ACTION_VIEW);
                FileProvider7.setIntentDataAndType(context,
                        intent, "application/vnd.android.package-archive", new File(Constant.SaveAPkFile), true);
                context.startActivity(intent);
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                Toast.makeText(context, "网络未知错误", Toast.LENGTH_SHORT).show();
                dismiss();
            }

            @Override
            public void onProgressChange(long fileSize, long downloadedSize) {
                super.onProgressChange(fileSize, downloadedSize);
                content.setGravity(Gravity.CENTER);
                content.setText((int) (downloadedSize * 1.0f / fileSize * 100)+"%");
            }
        });
    }
}
